﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ReportPrinting;

namespace BSE.Windows.Test
{
    public partial class HospitalReportForm : BaseForm, IReportMaker
    {
        private DateTimePicker startPicker = null;
        private int IsHospital = 1;

        public int isHospital
        {
            get { return IsHospital; }
            set
            {
                IsHospital = value;
                Text = IsHospital == 1 ? "住院处方登记表" : "门诊处方登记表";
            }
        }

        public HospitalReportForm()
        {
            InitializeComponent();
            InitToolStrip();
        }

        private void HospitalReportForm_Load(object sender, EventArgs e)
        {
            RefreshForm();
        }

        private void InitToolStrip()
        {
            startPicker = new DateTimePicker();
            startPicker.Width = 110;
            startPicker.Format = DateTimePickerFormat.Custom;
            startPicker.ValueChanged += new EventHandler(toolStripComboBoxDrug_SelectedIndexChanged);
            ToolStripControlHost host1 = new ToolStripControlHost(startPicker);
            toolStrip1.Items.Insert(0, host1);
        }

        public override void RefreshForm()
        {
            LoadChufangList();
        }

        private void LoadChufangList()
        {
            List<Chufang> chufangList = AnesthesiaBLL.GetAllChufangList(
                "",
                startPicker.Value, startPicker.Value,
                false, false, false);
            LoadChufangList(chufangList);
        }

        private void LoadChufangList(List<Chufang> chufangList)
        {
            listView1.Items.Clear();
            int i = 1;
            try
            {
                //i = AnesthesiaBLL.GetChufangIndex(isHospital, startPicker.Value) + 1;
            }
            catch (Exception e)
            {
                MessageBox.Show("读取数据失败！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            foreach (Chufang y in chufangList)
            {
                if (isHospital != y.IsHospital)
                    continue;

                ListViewItem lvit = new ListViewItem(y.Serial.ToString()/*i++.ToString()*/);
                lvit.Tag = y;

                if (++i % 2 == 1)
                    lvit.BackColor = Color.FromArgb(255, 192, 255, 255);
                else
                    lvit.BackColor = Color.FromArgb(255, 255, 255, 128);


                ListViewItem.ListViewSubItem lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.Lingyao.LingyaoDate.ToShortDateString();
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.PatientName;
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.PatientSex == 0 ? "女" : "男";
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.PatientAge.ToString();
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.PatientCardNo;
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.HospitalNo;
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.Illness;
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.Lingyao.LingyaoYaopin.YaopinName;
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.Lingyao.LingyaoYaopin.YaopinGuige;
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = (-y.Lingyao.LingyaoNumber).ToString();
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.Doctor.YishiName;
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.Lingyao.LingyaoCode;
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.TakerName;
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.TakerCardNo;
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.ReleaserName;
                lvit.SubItems.Add(lvsit);
                
                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.AuditorName;
                lvit.SubItems.Add(lvsit);
                
                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.BatchNo;
                lvit.SubItems.Add(lvsit);

                listView1.Items.Add(lvit);
            }
        }

        private void toolStripComboBoxDrug_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                LoadChufangList();
            }
            catch (Exception ex)
            {
                MessageBox.Show("读取数据失败！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void MakeDocument(ReportDocument reportDocument)
        {
            TextStyle.ResetStyles();

            TextStyle.Normal.Size = 10;
            TextStyle.Heading1.Size = 16;
            TextStyle.Heading1.Bold = false;
            TextStyle.TableHeader.BackgroundBrush = Brushes.White;// Brushes.Blue;
            TextStyle.TableHeader.Brush = Brushes.Black;//Brushes.LightGoldenrodYellow;
            TextStyle.TableHeader.MarginNear = 0.03f;// 0.1f;
            TextStyle.TableHeader.MarginFar = 0.03f;// 0.1f;
            TextStyle.TableHeader.MarginTop = 0.05f;
            TextStyle.TableHeader.MarginBottom = 0.05f;
            TextStyle.TableHeader.StringAlignment = StringAlignment.Center;
            TextStyle.TableRow.MarginNear = 0.03f;//0.1f;
            TextStyle.TableRow.MarginFar = 0.035f;//0.1f;
            TextStyle.TableRow.MarginTop = 0.05f;
            TextStyle.TableHeader.Size = 9;
            TextStyle.TableRow.Size = 9;

            ReportBuilder builder = new ReportBuilder(reportDocument);
            builder.StartLinearLayout(Direction.Vertical);

            Pen innerPen = new Pen(Color.Green, 0.02f);

            int year = startPicker.Value.Year;
            int month = startPicker.Value.Month;
            int day = startPicker.Value.Day;
            string header = isHospital == 1 ? "麻精药品住院处方登记表\n" : "麻精药品门诊处方登记表\n";
            builder.AddPageHeader(String.Empty, header + year + " 年  " + month + " 月  " + day + " 日", "第 %p 页");
            // Following line sets up the pen used for lins for tables
            //builder.AddPageBreak(PageOrientation.Landscape);
            builder.DefaultTablePen = reportDocument.ThinPen;
            DataView dv = GetDataView();
            builder.AddTable(dv, true);
            builder.CurrentSection.HorizontalAlignment = ReportPrinting.HorizontalAlignment.Center;
            builder.AddColumn("Serial", "序\n号", 1.0f, true, true);
            builder.AddColumn("PatientName", "患者\n姓名", 1.8f, true, true);
            builder.AddColumn("PatientSex", "性\n别", 1.0f, true, true);
            builder.AddColumn("PatientAge", "年\n龄", 1.0f, true, true);
            builder.AddColumn("PatientCardNo", "身份证号", 3.0f, true, true);
            string codeHeader = isHospital == 1 ? "住院\n号" : "门诊\n号";
            builder.AddColumn("HospitalNo", codeHeader, 1.8f, true, true);
            builder.AddColumn("Illness", "诊断", 3.6f, true, true);
            builder.AddColumn("YaopinName", "药品\n名称", 2.0f, true, true);
            builder.AddColumn("YaopinGuige", "规格", 1.8f, true, true);
            builder.AddColumn("YaopinDanwei", "单\n位", 1.0f, true, true);
            builder.AddColumn("Number", "数\n量", 1.0f, true, true);
            builder.AddColumn("YishiName", "医师\n姓名", 1.8f, true, true);
            builder.AddColumn("Code", "处方\n号", 1.8f, true, true);
            builder.AddColumn("TakerName", "取药人", 1.8f, true, true);
            //builder.AddColumn("TakerCardNo", "身份\n证号", 3.6f, true, true);
            builder.AddColumn("ReleaserName", "发药人", 1.8f, true, true);
            builder.AddColumn("AuditorName", "复核人", 1.8f, true, true);
            builder.AddColumn("BatchNo", "批号", 1.8f, true, true);
            builder.Table.OuterPens = reportDocument.NormalPen;

            builder.FinishLinearLayout();
            reportDocument.SetPageOrientation(1, PageOrientation.Landscape);
        }

        private DataView GetDataView()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Serial");
            dt.Columns.Add("PatientName");
            dt.Columns.Add("PatientSex");
            dt.Columns.Add("PatientAge");
            dt.Columns.Add("PatientCardNo");
            dt.Columns.Add("HospitalNo");
            dt.Columns.Add("Illness");
            dt.Columns.Add("YaopinName");
            dt.Columns.Add("YaopinGuige");
            dt.Columns.Add("YaopinDanwei");
            dt.Columns.Add("Number");
            dt.Columns.Add("YishiName");
            dt.Columns.Add("Code");
            dt.Columns.Add("TakerName");
            dt.Columns.Add("TakerCardNo");
            dt.Columns.Add("ReleaserName");
            dt.Columns.Add("AuditorName");
            dt.Columns.Add("BatchNo");

            foreach (ListViewItem item in listView1.Items)
            {
                Chufang cf = item.Tag as Chufang;
                DataRow dr = dt.NewRow();
                dr["Serial"] = cf.Serial;
                dr["PatientName"] = cf.PatientName;
                dr["PatientSex"] = cf.PatientSex == 0 ? "女" : "男";
                dr["PatientAge"] = cf.PatientAge.ToString(); ;
                dr["PatientCardNo"] = cf.PatientCardNo;
                dr["HospitalNo"] = cf.HospitalNo;
                dr["Illness"] = cf.Illness;
                dr["YaopinName"] = cf.Lingyao.LingyaoYaopin.YaopinName;
                dr["YaopinGuige"] = cf.Lingyao.LingyaoYaopin.YaopinGuige;
                dr["YaopinDanwei"] = cf.Lingyao.LingyaoYaopin.YaopinDanwei;
                dr["Number"] = (-cf.Lingyao.LingyaoNumber).ToString();
                dr["YishiName"] = cf.Doctor.YishiName;
                dr["Code"] = cf.Lingyao.LingyaoCode;
                dr["TakerName"] = cf.TakerName;
                dr["TakerCardNo"] = cf.TakerCardNo;
                dr["ReleaserName"] = cf.ReleaserName;
                dr["AuditorName"] = cf.AuditorName;
                dr["BatchNo"] = cf.BatchNo;
                dt.Rows.Add(dr);
            }

            return dt.DefaultView;
        }
    }
}
